import { Injectable, InternalServerErrorException } from '@nestjs/common';
import { PrismaService } from 'src/services/prisma.service';

@Injectable()
export class CommentsService {
  constructor(private readonly prisma: PrismaService) {}
  async getCommentList(
    page: number,
    pageSzie: number,
    commentNickname?: string,
    comment?: string,
  ) {
    const where = {
      AND: {
        comment: { contains: comment ?? '' },
        comment_nickname: { contains: commentNickname ?? '' },
      },
    };
    return await Promise.all([
      this.prisma.comments.findMany({
        skip: (page - 1) * pageSzie,
        take: pageSzie,
        where,
      }),
      this.prisma.comments.count({
        where,
      }),
    ]);
  }

  async deleteComment(commentId: number) {
    try {
      await this.prisma.comments.delete({
        where: { comment_id: commentId },
      });
    } catch (error) {
      throw new InternalServerErrorException('删除失败');
    }
  }
}
